/*
 * @Author: szx
 * @Date: 2022-03-24 14:09:17
 * @LastEditTime: 2022-03-24 14:17:41
 * @Description:
 * @FilePath: \leetcode\600-699\661\661.js
 */
/**
 * @param {number[][]} img
 * @return {number[][]}
 */
var imageSmoother = function (img) {
    const m = img.length;
    const n = img[0].length;
    const res = new Array(m).fill(0).map(() => new Array(n).fill(0));
    const dirs = [
        [-1, -1],
        [-1, 0],
        [-1, 1],
        [0, -1],
        [0, 0],
        [0, 1],
        [1, -1],
        [1, 0],
        [1, 1]
    ];
    let tmp = 0;
    let cnt = 0;
    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            tmp = 0;
            cnt = 0;
            for (let d of dirs) {
                const x = i + d[0];
                const y = j + d[0];
                if (x < 0 || x >= m || y < 0 || y >= n) continue;
                tmp += img[x][y];
                cnt++;
            }
            res[i][j] = Math.floor(tmp / cnt);
        }
    }
    return res;
};
